package com.obrock;

/**
 * Created with IntelliJ IDEA.
 * User: Дмитрий Г. Кузнецов
 * Date: 18.09.12
 * Time: 10:48
 * To change this template use File | Settings | File Templates.
 */

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

/**
 * @author Rick
 */
public class ReportAnalytics extends Activity {

    TextView title;
    TextView amountDebt;
    TextView amountNormal;
    ImageView btnAction;
    TableLayout blockHeader;
    TableLayout container;

    Account account = null;


    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.analytics);

        int bgColor = getResources().getColor(R.color.background_color);

        title = (TextView) findViewById(R.id.title);
        amountDebt = (TextView) findViewById(R.id.amount_debt);
        amountNormal = (TextView) findViewById(R.id.amount_normal);
        blockHeader = (TableLayout) findViewById(R.id.block_header);
        container = (TableLayout) findViewById(R.id.container);

        btnAction = (ImageView) findViewById(R.id.btnAction);
        btnAction.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });

        //Button btnCancel = (Button) findViewById(R.id.btnCancel);
        //btnCancel.setOnClickListener(this);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            account = (Account) extras.getSerializable("account");
        }

        if (account == null || account.accountID == -1 || account.analytics == null || account.analytics.size() == 0) {
            finish();
            return;
        }

        double balanceDebt = 0.0;
        double balanceCredit = 0.0;

        int i = 0;
        for (Analytics a : account.analytics) {

            balanceDebt += a.balanceDebt;
            balanceCredit += a.balanceCredit;

            TextView section = Main.getTextView(this, a.name);
            section.setTextSize(18);
            section.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
            section.setTextColor(getResources().getColor(R.color.titles_color));

            TextView amountD = Main.getTextView(this, String.format("%.2f", a.balanceDebt));
            amountD.setGravity(Gravity.RIGHT);
            amountD.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
            amountD.setTextSize(18);
            amountD.setTextColor(getResources().getColor(R.color.ac_normal_color));

            TextView amountC = Main.getTextView(this, String.format("%.2f", a.balanceCredit));
            amountC.setGravity(Gravity.RIGHT);
            amountC.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
            amountC.setTextSize(18);
            amountC.setTextColor(getResources().getColor(R.color.ac_debt_color));


            TableRow xrow = Main.addRow(this, bgColor, i++, new View[]{
                    section,
                    amountD,
                    amountC
            });

            container.addView(xrow);

            for (Analytics aa : a.values) {
                TextView subSection = Main.getTextView(this, "    " + aa.name);
                subSection.setTextSize(14);
                subSection.setTextColor(getResources().getColor(R.color.text_color));

                TextView subAmountD = Main.getTextView(this, String.format("%.2f", aa.balanceDebt));
                subAmountD.setGravity(Gravity.RIGHT);
                subAmountD.setTextSize(14);
                subAmountD.setTextColor(getResources().getColor(R.color.ac_normal_color));

                TextView subAmountC = Main.getTextView(this, String.format("%.2f", aa.balanceCredit));
                subAmountC.setGravity(Gravity.RIGHT);
                subAmountC.setTextSize(14);
                subAmountC.setTextColor(getResources().getColor(R.color.ac_debt_color));


                TableRow srow = Main.addRow(this, bgColor, i++, new View[]{
                        subSection,
                        subAmountD,
                        subAmountC
                });

                container.addView(srow);

            }
        }

        title.setText(account.name);
        amountNormal.setText(String.format("%.2f", balanceDebt));
        amountDebt.setText(String.format("%.2f", balanceCredit));

    }
}
